﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Png;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Tasks;


namespace ThuPhap.Class
{
    public class ExportImage
    {
        private string filenameSource = string.Empty;
        private string filename = string.Empty;
        private double width = 0;
        private double height = 0;

        public ExportImage()
        {
            this.filenameSource = "temp.jpg";
        }



        public void Export(Canvas canvas)
        {
            //this.Export(canvas, new PngEncoder());
           // try
            //{
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.width = canvas.Width;
                    this.height = canvas.Height;
                    this.exportPNG(canvas, new PngEncoder());
                    //this.saveAsJPGToIsolated();
                    //this.saveToSavedImagePhone();

                    //return "1";
                });
               // return "1";
            //}
            //catch (Exception ex)
            //{
            //    return ex.Message;
            //}
        }

        private void exportPNG(Canvas canvas, IImageEncoder encoder)
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //string nameUri = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss");
                string filename = this.filenameSource;

                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filename);
                //Deployment.Current.Dispatcher.BeginInvoke(() =>
                //{
                    using (var stream = fileStream)
                    {
                        var image = canvas.ToImage();

                        encoder.Encode(image, stream);
                    }
                    fileStream.Close();
               // });

                
            }
        }

        public string saveAsJPGToIsolated()
        {
            //string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss");
            string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            this.filename = filename;
            string filenameSource = this.filenameSource;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(filename))
                {
                    myIsolatedStorage.DeleteFile(filename);
                }
                BitmapImage bitmap = new BitmapImage();
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filename);

                IsolatedStorageFileStream fileStreamSource = myIsolatedStorage.OpenFile(filenameSource, FileMode.Open, FileAccess.Read);
                bitmap.SetSource(fileStreamSource);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, (int)this.width, (int)this.height, 0, 100);

                fileStream.Close();
                fileStreamSource.Close();
                if (myIsolatedStorage.FileExists(filenameSource))
                {
                    myIsolatedStorage.DeleteFile(filenameSource);
                }
            }
            return filename;
        }

        public void saveToSavedImagePhone()
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(this.filename, FileMode.Open, FileAccess.Read))
                {
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    Picture pic = mediaLibrary.SavePicture(DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg", fileStream);
                    fileStream.Close();
                }

                //PhotoChooserTask photoChooserTask = new PhotoChooserTask();
                //photoChooserTask.Show();
            }
        }

        public void ExportUsingWriteableBitmap(UIElement Element)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                Transform tranform = Element.TransformToVisual(Element) as Transform;
                WriteableBitmap writeableBipmap = new WriteableBitmap(Element, tranform);
                //string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss");
                //string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                //this.filename = filename;
                // string filenameSource = this.filenameSource;
                string filename = "temp.jpg";
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(filename))
                    {
                        myIsolatedStorage.DeleteFile(filename);
                    }
                    //BitmapImage bitmap = new BitmapImage();
                    IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filename);

                    //IsolatedStorageFileStream fileStreamSource = myIsolatedStorage.OpenFile(filenameSource, FileMode.Open, FileAccess.Read);
                    //bitmap.SetSource(fileStreamSource);
                    //WriteableBitmap wb = new WriteableBitmap(bitmap);

                    // Encode WriteableBitmap object to a JPEG stream.
                    Extensions.SaveJpeg(writeableBipmap, fileStream, writeableBipmap.PixelWidth, writeableBipmap.PixelHeight, 0, 100);

                    fileStream.Close();
                    //fileStreamSource.Close();
                    //if (myIsolatedStorage.FileExists(filenameSource))
                    //{
                    //    myIsolatedStorage.DeleteFile(filenameSource);
                    //}
                }
            });
            //return filename;
        }

        public string SaveAsJPGToIsolated()
        {
            //string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss");
            string filename = "thuphap" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            string filenameSource = "temp.jpg";
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(filename))
                {
                    myIsolatedStorage.DeleteFile(filename);
                }
                BitmapImage bitmap = new BitmapImage();
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filename);

                IsolatedStorageFileStream fileStreamSource = myIsolatedStorage.OpenFile(filenameSource, FileMode.Open, FileAccess.Read);
                bitmap.SetSource(fileStreamSource);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);

                fileStream.Close();
                fileStreamSource.Close();
                //if (myIsolatedStorage.FileExists(filenameSource))
                //{
                //    myIsolatedStorage.DeleteFile(filenameSource);
                //}
            }
            return filename;
        }

        public string ExportUsingWriteableBitmap(WriteableBitmap WriteableBitmap)
        {
            WriteableBitmap writeableBipmap = WriteableBitmap;
            string filename = "TP_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(filename))
                {
                    myIsolatedStorage.DeleteFile(filename);
                }
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filename);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(writeableBipmap, fileStream, writeableBipmap.PixelWidth, writeableBipmap.PixelHeight, 0, 100);

                fileStream.Close();
            }
            return filename;
        }

        public string SaveToSavedImagePhone(string Filename)
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(Filename, FileMode.Open, FileAccess.Read))
                {
                    string filenameDestination = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                    string filenameSource = Filename;
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    Picture pic = mediaLibrary.SavePicture(filenameDestination, fileStream);
                    fileStream.Close();
                    return filenameDestination;
                }

                //PhotoChooserTask photoChooserTask = new PhotoChooserTask();
                //photoChooserTask.Show();
            }
        }
    }
}
